################################################################################
## Group Identities and Parliamentary Debates: Replication package
## Fiva, Nedregård and Øien (2025)

## Description:

# Code to make figure 1: "Descriptive representation over time"

################################################################################

# Packages

library(data.table)
library(dplyr)
library(showtext)
library(ggpubr)
library(ggplot2)

# Directories (the working directory is ./scripts, set by master.R)

data.dir <-  "../data/1_raw_data"
fig.dir  <-  "../results/figures"

# Data
d <- fread(paste(data.dir, "meta_1945_2021.csv", sep = "/"))
d.pop <- fread(paste(data.dir, "population_demographics.csv", sep = "/"))




# Plot function

p_function <- function(data, x_var, y_var, y_lab, title){
  
  ggplot(data, aes(x = !!sym(x_var), y = !!sym(y_var), fill = as.factor(group), shape = as.factor(group))) +
    geom_line() +
    geom_point(colour = "black", size = 4) +
    scale_fill_manual(breaks = c("Left-wing legislators", "Right-wing legislators", "Population"),
                      values = c("Left-wing legislators" = "grey", "Right-wing legislators" = "black", "Population" = "white")) +
    scale_shape_manual(breaks = c("Left-wing legislators", "Right-wing legislators", "Population"),
                       values = c("Left-wing legislators" = 22, "Right-wing legislators" = 23, "Population" = 21)) +
    
    
    theme(
      panel.grid = element_blank(),
      panel.border = element_blank(),
      plot.margin = unit(c(1, 0.5, 0.5, 0.5), "cm"),
      panel.grid.major.y= element_line(linewidth = 0.2, colour = "grey90"),
      panel.background = element_rect(fill = "transparent"),
      plot.background = element_rect(fill = "transparent"),
      plot.title = element_text(family = "Montserrat", color = "black", size = 20, hjust = 0.5, 
                                margin=margin(0,0,10,0),),
      panel.grid.major.x = element_blank(),
      strip.text = element_text(family = "Montserrat", color = "black", size = 14),
      strip.background = element_blank(),
      axis.ticks.length = unit(0, "lines"),
      axis.text.y = element_text(family = "Montserrat", size = 12),
      axis.title.y = element_text(family = "Montserrat", size = 14, margin=margin(0,15,0,0)),
      axis.text.x = element_text(family = "Montserrat", size = 12, angle = 45, hjust = 1),
      axis.title.x = element_text(family = "Montserrat", size = 14, margin=margin(10,0,0,0)),
      panel.spacing = unit(2, "lines"),
      legend.position = "none",
      legend.title = element_blank(),
      legend.text = element_text(size = 14),
      legend.key.size = unit(3, "cm"),
      legend.key = element_blank()
      ) +
    scale_x_continuous(breaks = seq(1945, 2017, 4)) +
    labs(x = "Election year", y = y_lab, title = title)
  
  
  
  
}


# Data wrangling

d <- d |>
  mutate(year = election_year,
         left = 1*(bloc == "V"))

## AGE ##

d.rep <-  d %>%
  group_by(year, left) %>%
  summarise(age = median(age), .groups = "drop") %>%
  mutate(group = case_when(left == 1 ~ "Left-wing legislators", TRUE ~ "Right-wing legislators")) %>%
  select(year, age, group)


d.age <- d.pop |> 
  select(year, group, age = median_age) |>
  filter(!is.na(age))

d.age <- rbindlist(list(d.rep, d.age), use.names = T) 

## Gender ##

d.gender <- d %>%
  group_by(year, left) %>%
  summarise(female_MPs = mean(as.numeric(female)), 
            .groups = "drop") %>%
  mutate(group = case_when(left == 1 ~ "Left-wing legislators", TRUE ~ "Right-wing legislators")) %>%
  select(year, group, female = female_MPs)

d.pop.gender <- d.pop |>
  select(year, group, female = share_female) |>
  filter(!is.na(female))

d.gender <- rbindlist(list(d.gender, d.pop.gender))

## Urbanicity ##

d.urban <- d %>% 
  mutate(town = 1*(town == "urban")) %>%
  group_by(year, left) %>%
  summarise(town_MPs  = mean(town, na.rm = T), .groups = "drop") %>%
  mutate(group = case_when(left == 1 ~ "Left-wing legislators", TRUE ~ "Right-wing legislators")) %>%
  select(year, group, town = town_MPs)

d.pop.urban <- d.pop |>
  select(year, group, town = share_urban) |>
  filter(!is.na(town))

d.urban <- rbindlist(list(d.urban, d.pop.urban))

## Father occupation ##

d.class <- d %>%
  mutate(white = 1*(occupation == "white")) %>%
  filter(!is.na(white)) %>%
  group_by(year, left) %>%
  summarise(white = mean(white, na.rm = T), .groups = "drop") %>%
  mutate(group = case_when(left == 1 ~ "Left-wing legislators", TRUE ~ "Right-wing legislators")) %>%
  select(year, group, white) 

d.pop.class <- d.pop |>
  select(year, group, white = share_white_collar) |>
  filter(!is.na(white))

d.class <- rbindlist(list(d.class, d.pop.class))


# Plotting

# Add montserrat font

font_add_google(name = "Montserrat", family = "Montserrat")
showtext_auto()

p.gender <- p_function(d.gender, x_var = "year", y_var = "female", title = "Gender", y_lab = "Share women") +
  scale_y_continuous(limits = c(0, .8), breaks = seq(0, .8, .2))

p.age <- p_function(d.age, x_var = "year", y_var = "age", y_lab = "Median Age", title = "Age") +
  scale_y_continuous(limits = c(30, 60), breaks = seq(30, 60, 5))

p.urban <- p_function(d.urban, x_var = "year", y_var = "town", y_lab = "Share urban", title = "Urban") +
  scale_y_continuous(limits = c(0, .8), breaks = seq(0, .8, .2))

p.background <- p_function(d.class, x_var = "year", y_var = "white", y_lab = "Share white collar", 
                           title = "White-collar background") +
  scale_y_continuous(limits = c(0, .8), breaks = seq(0, .8, .2))

p.figure.1 <- ggarrange(p.gender, p.age, p.urban, p.background, ncol=2, nrow=2, 
                        common.legend = TRUE, legend="bottom")

ggsave(plot = p.figure.1, file = paste(fig.dir, "fig1.pdf", sep = "/"),
       width = 11, height = 9, pointsize = 12, device = cairo_pdf)








